const { Sequelize } = require('sequelize');
const CONFIG = require('../config/mysql.config')

const sequelize = new Sequelize(CONFIG.DBNAME, CONFIG.USERNAME, CONFIG.PASSWORD, {
  dialect: 'mariadb',
  host: CONFIG.HOST,
  port: CONFIG.PORT,
  define: {
    // 关闭表名推断
    freezeTableName: false,

    // 设置字符编码
    charset: 'utf8',

    // 设置字符集
    dialectOptions: {
      collate: 'utf8_general_ci'
    },
  }
});

// 测试数据库连接是否正常

(async function () {
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');
  } catch (error) {
    console.error('Unable to connect to the database:', error);
  }
})()


module.exports = sequelize